<template>
  <ConfigProvider :locale="getAntdLocale">
    <AppProvider>
      <RouterView />
    </AppProvider>
  </ConfigProvider>
</template>

<script lang="ts" setup>
  import { ConfigProvider } from 'ant-design-vue';
  import { AppProvider } from '/@/components/Application';
  import { useTitle } from '/@/hooks/web/useTitle';
  import { useLocale } from '/@/locales/useLocale';
  import { onMounted, ref } from 'vue';
  import { useUserStore } from '/@/store/modules/user';
  import Fingerprint2 from 'fingerprintjs2';




  import 'dayjs/locale/zh-cn';
  // support Multi-language
  const { getAntdLocale } = useLocale();

  // Listening to page changes and dynamically changing site titles
  useTitle();

  const loading = ref(false);
  const userStore = useUserStore();


  // 获取浏览器指纹匿名登录
  async function handleLogin() {
    Fingerprint2.get(async function (components) {
      const values = components.map(function (component, index) {
        if (index === 0) { //把微信浏览器里UA的wifi或4G等网络替换成空,不然切换网络会ID不一样
          return component.value.replace(/\bNetType\/\w+\b/, '');
        }
        return component.value;
      });
      // 生成最终id murmur
      let browserUuid = Fingerprint2.x64hash128(values.join(''), 31);
      console.log("指纹",browserUuid);

      try {
        loading.value = true;
        const userInfo = await userStore.login({
          password: '',
          username: '',
          browserUuid: browserUuid,
          anonymous: 2,
          mode: 'none', //不要默认的错误提示
          goHome: false,
        });
        if (userInfo) {
          console.log("匿名登录成功");
        }
      } catch (error) {
        console.error("匿名登录失败");
      } finally {
        loading.value = false;
      }

    });
  }

  /**
   * 初始化数据
   */
  onMounted(async () => {
    console.log("首页执行匿名登录");
    handleLogin();
  });
</script>
